package com.bootdo.system.controller;

import java.io.IOException;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.MultipartFile;
import com.bootdo.system.domain.NewsDO;
import com.bootdo.system.service.NewsService;
import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.FileUtil;
import com.bootdo.common.utils.OssUtil;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

@Controller
@RequestMapping("/system/news")
public class NewsController {
	@Autowired
	private NewsService newsService;
	@Autowired
	private BootdoConfig bootdoConfig;

	@GetMapping()
	String News() {
		return "system/news/news";
	}

	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		// 查询列表数据
		Query query = new Query(params);
		List<NewsDO> newsList = newsService.list(query);
		for (NewsDO newsDO : newsList) {
			String timeBefore = DateUtils.getTimeBefore(newsDO.getCreateTime());
			newsDO.setTimeBefore(timeBefore);
		}
		int total = newsService.count(query);
		PageUtils pageUtils = new PageUtils(newsList, total);
		return pageUtils;
	}

	/**
	 * @Title: applist
	 * @Description: TODO(移动端新闻详情接口)
	 * @author: lzy
	 * @date: 2018年5月30日 下午3:51:34
	 */
	@ResponseBody
	@GetMapping("/applist")
	NewsDO applist(@RequestParam("id") int id) {
		// 查询列表数据
		NewsDO newsDO = newsService.get(id);
		return newsDO;
	}

	@GetMapping("/add")
	String add() {
		return "system/news/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Integer id, Model model) {
		NewsDO news = newsService.get(id);
		model.addAttribute("news", news);
		return "system/news/edit";
	}

	/**
	 * @Title: save
	 * @Description: TODO(新闻图片保存的方法)
	 * @author: lzy
	 * @date: 2018年5月29日 下午7:49:28
	 */
	@ExceptionHandler(MultipartException.class)
	@ResponseBody
	@PostMapping("/save")
	public R save(@RequestParam("file") MultipartFile file, NewsDO newsDO) {
		String URL = null;
		try {
			URL = OssUtil.fileUpload(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		newsDO.setUrl(URL); // 服务端
		newsDO.setCreateTime(new Date());
		if (newsService.save(newsDO) > 0) {
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update(@RequestParam("file") MultipartFile file, NewsDO newDo) {
		String URL = null;
		try {
			URL = OssUtil.fileUpload(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String imgUrl = newsService.get(newDo.getId()).getUrl().substring(45, 62);
		OssUtil.deleteBucketInfo(imgUrl);

		newDo.setUrl(URL);
		int update = newsService.update(newDo);

		if (update == 1) {
			return R.ok("修改成功");
		} else {
			return R.error("修改失败");
		}

	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ResponseBody
	public R remove(@RequestParam("id") int id, HttpServletRequest request) {
		String imgUrl =newsService.get(id).getUrl().substring(45, 62);
		if (newsService.remove(id) > 0) {
			OssUtil.deleteBucketInfo(imgUrl);
			return R.ok();
		} else {
			return R.error();
		}
		
	
		
	}

	/**
	 * 删除
	 */
	@PostMapping("/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:news:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids) {
		newsService.batchRemove(ids);
		return R.ok();
	}

}
